<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 登录图标区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登陆表单区域 -->
            <el-form ref="LoginForm" :model="loginForm" :rules="rules" label-width="80px" class="login_form">
                <el-form-item  label="账号"  prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item  label="密码" prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetForm('LoginForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入登录名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            resetForm(forName){
                this.$refs[forName].resetFields();
                //或者 this.$refs.LoginForm.resetFields();
            },
            login(){
                // this.$refs.LoginForm.validate((valid) => {}) valid是一个布尔类型的值
                console.log(111);
                this.$refs.LoginForm.validate(async valid => {
                    if(!valid) return;
                    const  { data: res } = await this.$http.post("login",this.loginForm);
                    console.log('result',res);
                    if(res.meta.status !== 200) {
                        this.$message.error("登陆失败")
                    } else {
                        this.$message({
                            message: "登陆成功",
                            type:"success"
                        })
                        // 1.将登录之后的token保存到客户端的sessionStore中
                        //   1.1项目中除了登陆以外的PAPI接口，必须在登陆后才能访问
                        //   1.2token只有在当前网站打开期间生效，所以保存在sessionStore中
                        // 2.通过编程式导航跳转到后台主页，路由地址是'/home'
                        window.sessionStorage.setItem('token',res.data.token);
                        this.$router.push("/home")
                    }
                    // if(res.meta.status !== 200) return console.log("登陆失败");
                    // else{console.log("登陆成功");}
                    // 如果某个方法的返回值是promise，我们可以使用async await来简化这次操作
                    // await只能用在被async修饰的方法中
                    // const result = this.$http.post("login",loginForm)
                    // const result = await this.$http.post("login",loginForm) { data:res }使用了解构
                    
                })
            }
        }
    }
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;
}
.login_box {
    height: 300px;
    width: 450px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%); 
    
    .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd ;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);        //translate的百分比是按照盒子的宽高度
        background-color: #fff;
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
    .btns {
        display: flex;
        justify-content: flex-end;
    }
    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 10px;
        box-sizing: border-box;
    }
}


</style>
